/************************************************************************
*
*    File Name: widget.cpp
*    Description: 编写菜单界面    点击开始游戏时将本界面的玩家名称传过去，得分可直接通过游戏界面获取，这样就可以在游戏界面实现数据插入
*               游戏设置按钮未实现
*    Version: V1.0
*    Author: 王涵
*    Create Time: 2024-11-27
*
*************************************************************************/
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(MAIN_WIDTH,MAIN_HEIGHT);
    this->setWindowTitle(QString(MAIN_TITLE));
    this->setWindowIcon(QIcon(GAME_ICON));

    QLabel *label = new QLabel(this);
    label->resize(MAIN_WIDTH,MAIN_HEIGHT);
    QPixmap pixmap = QPixmap(QString(MAIN_BACKGROUND));
    pixmap = pixmap.scaled(MAIN_WIDTH,MAIN_HEIGHT);
    label->setPixmap(pixmap);

     QLabel *playerlabel = new QLabel(this);
    playerlabel->resize(100,50);
    playerlabel->move(30,0);
    playerlabel->setText("玩家名称:");
    playerlabel->setStyleSheet(
                "font-size : 18px;"
                "color : white;"
                );
    playername = new QLineEdit(this);
    playername->resize(120,30);
    playername->move(125,10);
    playername->setText("player1");
    playername->setAlignment(Qt::AlignCenter);
    playername->setPlaceholderText("输入玩家名称");
    playername->setStyleSheet(
                "font-size : 18px;"
                "border-radius:10px;"
                );
    QPushButton *bStart = new QPushButton(this);
    bStart->setText("开始游戏");bStart->move(200,280);bStart->resize(140,50);
    bStart->setStyleSheet(BUTTON_STYLE);
    QPushButton *bScoreBoard = new QPushButton(this);
    bScoreBoard->setText("积分榜");bScoreBoard->move(200,350);bScoreBoard->resize(140,50);
    bScoreBoard->setStyleSheet(BUTTON_STYLE);
    QPushButton *bQuit = new QPushButton(this);
    bQuit->setText("退出游戏");bQuit->move(200,420);bQuit->resize(140,50);
    bQuit->setStyleSheet(BUTTON_STYLE);

    connect(bStart,&QPushButton::clicked,this,&Widget::on_bStart_clicked);
    connect(bQuit,&QPushButton::clicked,this,&Widget::on_bQuit_clicked);
    connect(bScoreBoard,&QPushButton::clicked,this,&Widget::on_bScoreBoard_clicked);
}

Widget::~Widget()
{
    delete ui;
}

// 退出按钮
void Widget::on_bQuit_clicked(){
    this->close();
}
// 退出游戏slot  (玩家阵亡时调用该slot)
void Widget::game_close_slot()
{
    game->insert_data(user_name);
    game->close();
}

void Widget::setting_slot(int map_path, int plane_path)
{
    game = new GameWidget(map_path,plane_path);
    user_name = this->playername->text(); // 获取玩家姓名
    this->close();
    game->show();// //////////////////////////////////////
    connect(game,&GameWidget::game_close,this,&Widget::game_close_slot);
}
// 开始按钮
void Widget::on_bStart_clicked()
{
    setting_widget = new SettingWidget;
    this->close();
    setting_widget->show();
    connect(setting_widget,&SettingWidget::setting_ok,this,&Widget::setting_slot); // 游戏设置
}
// 得分榜
void Widget::on_bScoreBoard_clicked(){
    ScoreBoard *board = new ScoreBoard;
    board->show();
}

void SettingWidget::on_pushButton_clicked()
{
    emit setting_ok(map_path,player_path);
    this->close();
}

